using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Cryptography;
using System.Threading;

namespace Beetle;

public class TcpUtils
{
	internal static bool a;

	private static l m_b;

	internal static object c;

	internal static object d;

	internal static ay e;

	internal static o f;

	internal static o g;

	private static IList<at> m_h;

	private static IList<at> i;

	private static IList<at> j;

	public static int SocketDespatchSleepTime;

	public static int WorkDespatchSleepTime;

	public static bool Statistics;

	public static long SendBytes;

	public static long ReceiveBytes;

	public static long SendDataIO;

	public static long ReceiveDataIO;

	public static bool EnabledReceiveQueue;

	public static bool EnabledSendQueue;

	public static bool EnabledWorkQueue;

	public static int ChannelMaxQueueData;

	public static int DataPacketMaxLength;

	public static int SendPacketSpacing;

	public static int SendMessageSpacing;

	public static int SendBufferLength;

	public static int ReceiveBufferLength;

	public static int Connections;

	public static int StringEncodingLength;

	public static bool UseStringEncoding;

	public static int TimeOutms;

	public static bool ExecutionContext;

	public static int BufferPoolSize;

	public static long ReceiveMessages;

	public static long SendMessages;

	internal static int k;

	private static int l;

	private static int m;

	private static int n;

	[ThreadStatic]
	private static bool o;

	internal static string h()
	{
		return AppDomain.CurrentDomain.BaseDirectory;
	}

	static TcpUtils()
	{
		TcpUtils.a = false;
		TcpUtils.m_b = new l();
		TcpUtils.c = new object();
		TcpUtils.d = new object();
		TcpUtils.m_h = new List<at>();
		i = new List<at>();
		j = new List<at>();
		SocketDespatchSleepTime = 10;
		WorkDespatchSleepTime = 10;
		EnabledReceiveQueue = true;
		EnabledSendQueue = true;
		EnabledWorkQueue = true;
		ChannelMaxQueueData = 0;
		l = 0;
		m = 0;
		n = 0;
		o = false;
		DataPacketMaxLength = 8204;
		SendBufferLength = 2048;
		ReceiveBufferLength = 2048;
		k = 1;
		Connections = 1000;
		EnabledReceiveQueue = true;
		Statistics = false;
		SendPacketSpacing = 0;
		SendMessageSpacing = 0;
		StringEncodingLength = 512;
		UseStringEncoding = true;
		BufferPoolSize = 1000;
		ExecutionContext = false;
		TimeOutms = 30000;
		g();
	}

	private static void g()
	{
	}

	public static void Setup(string configname)
	{
		ConfigSection configSection = (ConfigSection)ConfigurationManager.GetSection(configname);
		if (configSection == null)
		{
			throw NetTcpException.ConfigSelectionNotFound(configname);
		}
		Statistics = configSection.Statistics;
		SocketDespatchSleepTime = configSection.SocketThreadSleep;
		WorkDespatchSleepTime = configSection.WorkThreadSleep;
		StringEncodingLength = configSection.StringEncodingSize;
		ReceiveBufferLength = configSection.ReceiveBufferSize;
		SendBufferLength = configSection.SendBufferSize;
		DataPacketMaxLength = configSection.PackageMaxSize;
		TimeOutms = configSection.TimeOut;
		ChannelMaxQueueData = configSection.ChannelMaxQueueData;
		ExecutionContext = configSection.ExecutionContext;
		EnabledReceiveQueue = configSection.EnabledReceiveQueue;
		EnabledSendQueue = configSection.EnabledSendQueue;
		EnabledWorkQueue = configSection.EnabledWorkQueue;
		Setup(configSection.Connections, configSection.Pools, configSection.SocketSendThreads, configSection.SocketReceiveThreads, configSection.WorkThreads);
	}

	public static void Setup(int maxconnections, int bufferpools, int senddespatchs, int receivedespatch, int workdespatchs)
	{
		Clean();
		Connections = maxconnections;
		TcpUtils.e = new ay(Connections, ReceiveBufferLength);
		a(Connections);
		for (int i = 0; i < senddespatchs; i++)
		{
			at at = new at();
			at.g = SocketDespatchSleepTime;
			TcpUtils.m_h.Add(at);
		}
		for (int j = 0; j < receivedespatch; j++)
		{
			at at = new at();
			at.g = SocketDespatchSleepTime;
			TcpUtils.i.Add(at);
		}
		for (int k = 0; k < workdespatchs; k++)
		{
			at at = new at();
			at.g = WorkDespatchSleepTime;
			TcpUtils.j.Add(at);
		}
		TcpUtils.a = true;
	}

	private static void a(int A_0)
	{
		if (TcpUtils.g != null)
		{
			TcpUtils.g.b();
		}
		if (TcpUtils.f != null)
		{
			TcpUtils.f.b();
		}
		TcpUtils.g = new o(A_0, SendBufferLength);
		TcpUtils.f = new o(A_0, ReceiveBufferLength);
	}

	public static void Setup(int maxconnections, int bufferpools, int despatchs)
	{
		Setup(maxconnections, bufferpools, despatchs, despatchs, despatchs);
	}

	public static void Clean()
	{
		if (TcpUtils.e != null)
		{
			TcpUtils.e.Dispose();
		}
		f();
		i.Clear();
		TcpUtils.m_h.Clear();
		j.Clear();
		TcpUtils.a = false;
	}

	private static void f()
	{
		foreach (at item in TcpUtils.m_h)
		{
			item.Dispose();
		}
		foreach (at item2 in i)
		{
			item2.Dispose();
		}
		foreach (at item3 in j)
		{
			item3.Dispose();
		}
	}

	internal static at e()
	{
		lock (TcpUtils.m_h)
		{
			l++;
			if (l >= TcpUtils.m_h.Count)
			{
				l = 0;
			}
			return TcpUtils.m_h[l];
		}
	}

	internal static at d()
	{
		lock (i)
		{
			m++;
			if (m >= i.Count)
			{
				m = 0;
			}
			return i[m];
		}
	}

	public static int GetAsyncEventPoolStatus()
	{
		return TcpUtils.e.b();
	}

	public static IList<int> GetSendDespatchStatus()
	{
		List<int> list = new List<int>();
		foreach (at item in TcpUtils.m_h)
		{
			list.Add(item.e());
		}
		return list;
	}

	public static IList<int> GetReceiveDespatchStatus()
	{
		List<int> list = new List<int>();
		foreach (at item in i)
		{
			list.Add(item.e());
		}
		return list;
	}

	public static IList<int> GetWorkDespatchsStatus()
	{
		IList<int> list = new List<int>();
		if (j != null)
		{
			for (int i = 0; i < j.Count; i++)
			{
				list.Add(j[i].e());
			}
		}
		return list;
	}

	public static int GetSendBufferStatus()
	{
		if (TcpUtils.f == null)
		{
			return 0;
		}
		return TcpUtils.f.c();
	}

	public static int GetReceiveBufferStatus()
	{
		if (TcpUtils.g == null)
		{
			return 0;
		}
		return TcpUtils.g.c();
	}

	public static void Setup(int maxonlineconnections, int despatchs)
	{
		Setup(maxonlineconnections, Convert.ToInt32(Math.Ceiling((double)maxonlineconnections / 1000.0)) * 2, despatchs);
	}

	public static void Setup(int maxonlineconnections)
	{
		Setup(maxonlineconnections, 1, 1);
	}

	internal static at c()
	{
		lock (j)
		{
			if (j == null || j.Count == 0)
			{
				throw NetTcpException.NotInitialize();
			}
			n++;
			if (n == j.Count)
			{
				n = 0;
			}
			return j[n];
		}
	}

	internal static object b()
	{
		return new RSACryptoServiceProvider(2048);
	}

	internal static void a()
	{
		if (!o && !ExecutionContext)
		{
			if (!System.Threading.ExecutionContext.IsFlowSuppressed())
			{
				System.Threading.ExecutionContext.SuppressFlow();
			}
			o = true;
		}
	}
}
